<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="车次id" prop="trainId">
      <el-input v-model="dataForm.trainId" placeholder="车次id"></el-input>
    </el-form-item>
    <el-form-item label="车次号" prop="trainCode">
      <el-input v-model="dataForm.trainCode" placeholder="车次号"></el-input>
    </el-form-item>
    <el-form-item label="车厢号" prop="carriageNumber">
      <el-input v-model="dataForm.carriageNumber" placeholder="车厢号"></el-input>
    </el-form-item>
    <el-form-item label="座位号" prop="seatNumber">
      <el-input v-model="dataForm.seatNumber" placeholder="座位号"></el-input>
    </el-form-item>
    <el-form-item label="车站区间id" prop="stationSectionId">
      <el-input v-model="dataForm.stationSectionId" placeholder="车站区间id"></el-input>
    </el-form-item>
    <el-form-item label="购票用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="购票用户id"></el-input>
    </el-form-item>
    <el-form-item label="车票状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="车票状态"></el-input>
    </el-form-item>
    <el-form-item label="始发站日期（生成日期）" prop="departureDate">
      <el-input v-model="dataForm.departureDate" placeholder="始发站日期（生成日期）"></el-input>
    </el-form-item>
    <el-form-item label="订单id" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单id"></el-input>
    </el-form-item>
    <el-form-item label="原始价格，单位分" prop="price">
      <el-input v-model="dataForm.price" placeholder="原始价格，单位分"></el-input>
    </el-form-item>
    <el-form-item label="票面价格(成交价格), 单位分" prop="transactionPrice">
      <el-input v-model="dataForm.transactionPrice" placeholder="票面价格(成交价格), 单位分"></el-input>
    </el-form-item>
    <el-form-item label="" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          trainId: '',
          trainCode: '',
          carriageNumber: '',
          seatNumber: '',
          stationSectionId: '',
          userId: '',
          status: '',
          departureDate: '',
          orderId: '',
          price: '',
          transactionPrice: '',
          createTime: '',
          updateTime: ''
        },
        dataRule: {
          trainId: [
            { required: true, message: '车次id不能为空', trigger: 'blur' }
          ],
          trainCode: [
            { required: true, message: '车次号不能为空', trigger: 'blur' }
          ],
          carriageNumber: [
            { required: true, message: '车厢号不能为空', trigger: 'blur' }
          ],
          seatNumber: [
            { required: true, message: '座位号不能为空', trigger: 'blur' }
          ],
          stationSectionId: [
            { required: true, message: '车站区间id不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '购票用户id不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '车票状态不能为空', trigger: 'blur' }
          ],
          departureDate: [
            { required: true, message: '始发站日期（生成日期）不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单id不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '原始价格，单位分不能为空', trigger: 'blur' }
          ],
          transactionPrice: [
            { required: true, message: '票面价格(成交价格), 单位分不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/product/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.trainId = data.product.trainId
                this.dataForm.trainCode = data.product.trainCode
                this.dataForm.carriageNumber = data.product.carriageNumber
                this.dataForm.seatNumber = data.product.seatNumber
                this.dataForm.stationSectionId = data.product.stationSectionId
                this.dataForm.userId = data.product.userId
                this.dataForm.status = data.product.status
                this.dataForm.departureDate = data.product.departureDate
                this.dataForm.orderId = data.product.orderId
                this.dataForm.price = data.product.price
                this.dataForm.transactionPrice = data.product.transactionPrice
                this.dataForm.createTime = data.product.createTime
                this.dataForm.updateTime = data.product.updateTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ticket/product/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'trainId': this.dataForm.trainId,
                'trainCode': this.dataForm.trainCode,
                'carriageNumber': this.dataForm.carriageNumber,
                'seatNumber': this.dataForm.seatNumber,
                'stationSectionId': this.dataForm.stationSectionId,
                'userId': this.dataForm.userId,
                'status': this.dataForm.status,
                'departureDate': this.dataForm.departureDate,
                'orderId': this.dataForm.orderId,
                'price': this.dataForm.price,
                'transactionPrice': this.dataForm.transactionPrice,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
